#include "libraries.h"
#include "extern.h"
#include "CCreature.h"
#include "Strike.h"
#include "lists.h"

void CCreature::SharpHit(int Damage)	{int a = HP - Damage + SharpDefense; if (a < HP) {HP = a;} else {HP--;} CStrike* p = new CStrike(X,Y,SprX,SprY);OL.push_front(p);} 
void CCreature::BluntHit(int Damage)	{int a = HP - Damage + BluntDefense; if (a < HP) {HP = a;} else {HP--;} CStrike* p = new CStrike(X,Y,SprX,SprY);OL.push_front(p);} 
void CCreature::CurseHit(int Damage)	{int a = HP - Damage + SharpDefense; if (a < HP) {HP = a;} else {HP--;} CStrike* p = new CStrike(X,Y,SprX,SprY);OL.push_front(p); Curse = 1;} 

void CCreature::RunRay(int ToX, int ToY)
{
 	double g = (ToX-X)*(ToX-X) + (ToY-Y)*(ToY-Y);
	/*if (sqrt(g) <= NearestEnemy.Distance)
	{
		int x = X;
		int y = Y;
		double XCounter = 0;
		double YCounter = 0;
		double a = 0.3 / sqrt(g);
		double XStep = a * (ToX-x);
		double Ystep = a * (ToY-y);
		while (x != ToX && y != ToY)
		{
			XCounter += XStep;
			YCounter += Ystep;
			if (XCounter >= 1) {if (Map[x+1][y] <= 1) {return;} else {x++; XCounter--;}}
			if (XCounter <= -1) {if (Map[x-1][y] <= 1) {return;} else {x--; XCounter++;}}
			if (YCounter >= 1) {if (Map[x][y+1] <= 1) {return;} else {y++; YCounter--;}}
			if (YCounter <= -1) {if (Map[x][y-1] <= 1) {return;} else {y--; YCounter++;}}
		}*/
		NearestEnemy.Distance = sqrt (g);
		NearestEnemy.X = ToX;
		NearestEnemy.Y = ToY;
	//}
}

bool CCreature::FindWay(int ToX, int ToY)
{
	if (Map[ToX][ToY] == 0) {return 0;}
	if (!findpath(X,Y,ToX,ToY)) {return 0;}
	else
	{
		for (int i = 0; i < 200; i++) {MyWay[i].X = Way[i].X; MyWay[i].Y = Way[i].Y;}
		MyWayEnd = WayEnd - 1;
		UseWay = 1;
	}
	return 1;
}

void CCreature::DrawFrame()
{
	int x, y;
	x = X-(plX-10);
	if(x>=0&&x<21)
	{
		y = Y-(plY-10);
		if(y>=0&&y<21)
		{
			if (ViewMap[x][y] == 1 || View == 0)
			{
				FramePicture.set(SprX-plX*32+LenghtToScreenMiddleX+(320-LenghtToScreenMiddleX)*2, SprY-plY*32+LenghtToScreenMiddleY+(320-LenghtToScreenMiddleY)*2);
				FramePicture.Draw();
			}
		}
	}
}